<template>
	<el-dialog
		:title="titleMap[mode]"
		v-model="visible"
		:width="600"
		destroy-on-close
		@closed="$emit('closed')"
	>
		<sk-tabs type="border-card">
			<el-tab-pane ref="tab" label="基础信息">
				<el-form
					:model="form"
					:rules="rules"
					ref="dialogForm"
					label-width="100px"
					label-position="right"
				>
					<el-row>
						<el-col :span="22">
							<el-form-item label="车间编号" prop="code">
								<el-input v-model="form.code"></el-input>
							</el-form-item>
						</el-col>

						<el-col :span="22">
							<el-form-item label="车间名" prop="name">
								<el-input v-model="form.name"></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="22">
							<el-form-item label="部门" prop="name">
								<skAutoChoose
									v-model="form.department.name"
									:name="'department'"
									:assign="form"
									:asValue="'0'"
								>
								</skAutoChoose>
							</el-form-item>
						</el-col>

						<el-col :span="22">
							<el-form-item label="备注" prop="remark">
								<el-input
									label="备注"
									v-model="form.remark"
								></el-input>
							</el-form-item>
						</el-col>
					</el-row>
				</el-form>
			</el-tab-pane>
			<el-tab-pane ref="tab1" label="工序">
				<sc-VxeTable
					ref="xGrid1"
					:tablecode="'pro_dept_save'"
					:data="form.process_dept"
				>
				</sc-VxeTable
			></el-tab-pane>
		</sk-tabs>

		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()"
				>保 存</el-button
			>
		</template>
	</el-dialog>
</template>

<script>
import skAutoChoose from "@/components/skAutoChoose";
import skDicSelect from "@/components/skDicSelect";
import tool from "@/utils/tool";
export default {
	emits: ["success", "closed"],
	components: { skAutoChoose, skDicSelect },
	data() {
		return {
			mode: "edit",

			titleMap: {
				add: "新增",
				edit: "编辑",
			},

			form: {
				department: {},
				process_dept: [],
			},
			rules: {
				code: [{ required: true, message: "编号必须填写" }],
				name: [{ required: true, message: "名称必须填写" }],
			},
			visible: false,
			isSaveing: false,
		};
	},
	mounted() {},
	methods: {
		//显示
		open(mode = "add") {
			this.mode = mode;
			this.visible = true;

			return this;
		},

		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = await this.$API.pro.dept.save.post(this.form);
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit("success", this.form, this.mode);
						this.visible = false;
						this.$message.success("操作成功");
					} else {
						this.$alert(res.message, "提示", { type: "error" });
					}
				}
			});
		},

		//表单注入数据
		setData(data) {
			Object.assign(this.form, tool.removeNull(data));
		},
	},
};
</script>
